﻿#

param
(
	[string]$endpoints = 'http://127.0.0.1:2379' #后面不要有/
)

if ( ($null -eq $endpoints) -or ($endpoints -eq '') )
{
	if ( ($null -eq $global:endpoints) -or ($global:endpoints -eq '') )
	{
		Write-Error '错误：endpoints值错误。返回码 21'
		exit 21
	}
	else
	{
		$endpoints = $global:endpoints
	}
}

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#问：这个脚本谁写的？有问题找谁技术支持?
#答：QQ群号=722528388
#名称=k4t国产k8s群1
#华之夏，脚之巅，有我ps1片天！
#专门教学win，linux通用的ps1脚本。不想学也可以，入群用红包求写脚本。

$private:params_memberlist = @{
	ContentType = 'application/x-www-form-urlencoded'
	Method      = 'POST'
	URI         = "${endpoints}/v3/cluster/member/list"
	TimeoutSec  = 4
}

$private:params_endpointsstates = @{
	ContentType = 'application/x-www-form-urlencoded'
	Method      = 'POST'
	URI         = "${endpoints}/v3/maintenance/status"
	TimeoutSec  = 4
}

$private:rl = Invoke-WebRequest @private:params_memberlist
$private:rs = Invoke-WebRequest @private:params_endpointsstates


$private:rl2 = $rl | ConvertFrom-Json
$private:rs2 = $rs.content | ConvertFrom-Json

#$private:rl2
# $private:rs2
# Exit
function gvs ($e)
{
	return $(& "${PSScriptRoot}/get-etcdVersion.ps1" -endpoints $e)
}

function isleader([string]$a,[string]$b)
{
	if ($a -eq $b)
	{
		return $true
	}
	else
	{
		return $false
	}
}

$rt = @()
foreach ($private:i in $private:rl2.members)
{
	$private:jk = & "${PSScriptRoot}/get-etcdHealth.ps1" -endpoints $endpoints -pfx $pfx
	$rt += [pscustomobject]@{
		节点id       = [string]$private:i.id
		节点名        = $private:i.name
		version    = gvs $private:i.clientURLs
		leader     = isleader $private:i.id $private:rs2.leader
		peerURLs   = $private:i.peerURLs.split(',')
		clientURLs = $private:i.clientURLs.split(',')
		健康         = $private:jk
	}
}
$private:gs = `
@{Expression = { $_.节点id };Label = "节点id";width = 28 },
@{Expression = { $_.节点名 };Label = "节点名";width = 16 },
@{Expression = { $_.健康 };Label = "健康" ;width = 6 },
@{Expression = { $_.version };Label = "version";width = 7 },
@{Expression = { $_.leader };Label = "leader";width = 6 },
@{Expression = { $_.peerURLs };Label = "peerURLs";width = 28 },
@{Expression = { $_.clientURLs };Label = "clientURLs";width = 28 }

$rt | Format-Table $private:gs -Wrap

exit 0

